package com.twu.biblioteca;

import java.util.List;

public class BibliotecaController {

    public String welcome() {
        return "Welcome to Biblioteca!\n";
    }

    public String displayOptions(List<LibraryOption> libraryOptionList) {
        String displayMessage = "";
        for (LibraryOption libraryOption : libraryOptionList) {
            displayMessage += libraryOption.getName() +"\n";
        }
        displayMessage += "Quit\n";
        return displayMessage;
    }

    public void quit() {
        System.exit(0);
    }

    public String checkoutBook(CheckoutBookOption checkoutBookOption, String bookName, User user) {
        int bookNumber = checkoutBookOption.getLibrary().getBooks().size();
        if (checkoutBookOption.libraryOption(new Book(bookName)).size() != bookNumber){
            for (Book book : checkoutBookOption.getLibrary().getLibraryBooks()){
                if (book.getName().equals(bookName)) {
                    book.setBorrowedUser(user);
                }
            }
            return "Thank you! Enjoy the book.";
        }
        return "That book is not available.";
    }

    public String returnBook(ReturnBookOption returnBookOption, String bookName) {
        int bookNumber = returnBookOption.getLibrary().getBooks().size();
        int size = returnBookOption.libraryOption(new Book(bookName)).size();
        if (size != bookNumber){
            returnBookOption.getLibrary().getReturnedBook(bookName).setBorrowedUser(null);
            return "Thank you for returning the book.";
        }
        return "That is not a valid book to return.";
    }

    public String listBook(ListingBookOption listingBookOption) {
        String bookList = "";
        for(Book b : listingBookOption.libraryOption()) {
            bookList += b.getName() +"\n";
        }
        return bookList;
    }

    public String invalidOption() {
        return "Select a valid option!";
    }

    public String listMovie(ListingMovieOption listingMovieOption) {
        String movieList = "";
        for (Movie movie : listingMovieOption.libraryOption()) {
            movieList += movie.toString();
        }
        return movieList;
    }

    public String checkoutMovie(CheckoutMovieOption checkoutMovieOption, String movieName) {
        for (Movie movie : checkoutMovieOption.getLibrary().getMovies()) {
            if (movieName.equals(movie.getName())){
                checkoutMovieOption.libraryOption(movie);
                return "Thank you! Enjoy the movie.";
            }
        }
        return "That movie is not available.";
    }

    public Boolean login(LoginOption loginOption, String username, String password) {
        if(loginOption.check(username, password)) {
            return true;
        }
        return false;
    }

    public String userInfo(UserInfoOption userInfoOption, String name, String password) {
        User user = userInfoOption.libraryOption(name, password);
        return user.getName() + " " + user.getEmail() + " " + user.getPhoneNo();
    }
}
